/*
 *  Copyright 2001-2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package ar.uba.fi.posgrado.academy.web.form.course;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.util.LabelValueBean;

import ar.uba.fi.posgrado.academy.model.Course;
import ar.uba.fi.posgrado.academy.model.Instalment;
import ar.uba.fi.posgrado.academy.model.Resolution;
import ar.uba.fi.posgrado.academy.model.Subject;

/**
 * Form para {@link Subject}
 * 
 * @author ariel.piechotka
 */
@SuppressWarnings("serial")
public class CourseForm extends ActionForm {

	private Long id;

	private String code;

	private String name;

	private String curriculum;

	private String certificate;

	private String firstDueDate;

	private Long idBody;

	// La resolution que agrega el usuario, si quiere
	private Resolution newResolution = new Resolution();

	private Resolution[] resolutions;

	private List<LabelValueBean> bodyOptions = new ArrayList<LabelValueBean>();

	private String courseTypeSelected;

	private List<LabelValueBean> courseTypeCollection = new ArrayList<LabelValueBean>();

	private List<Course> courses = new ArrayList<Course>();

	// Materias disponibles
	private List<Subject> availableSubjects = new ArrayList<Subject>();

	// Materias del curso
	private List<Subject> actualSubjects = new ArrayList<Subject>();

	private List<Instalment> instalments = new ArrayList<Instalment>();

	/**
	 * String con las materias elegidas separadas por ';'. Tener en cuenta q el
	 * delimitador esta en un archivo Javascript, por lo en caso de cambiarlo,
	 * mantener coherencia con el archivo js
	 */
	private String chosenSubjects;

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors actionErrors = new ActionErrors();

		if (this.name.equals(""))
			actionErrors.add("course.name", new ActionMessage(
					"course.name.empty"));
		if (this.curriculum.equals(""))
			actionErrors.add("course.curriculum", new ActionMessage(
					"course.curriculum.empty"));
		if (this.curriculum.length() > 255)
			actionErrors.add("course.curriculum", new ActionMessage(
					"course.curriculum.full"));
		if (this.certificate.length() > 255)
			actionErrors.add("course.certificate", new ActionMessage(
					"course.certificate.full"));
		if (this.idBody == null)
			actionErrors.add("course.body", new ActionMessage(
					"course.body.empty"));

		return actionErrors;
	}

	public void clear() {
		id = null;
		code = "";
		name = "";
		curriculum = "";
		certificate = "";
		idBody = null;
		courses = null;
		courseTypeSelected = "";
		availableSubjects = null;
		actualSubjects = null;
		firstDueDate = null;
	}

	public List<Subject> getActualSubjects() {
		return actualSubjects;
	}

	public void setActualSubjects(List<Subject> actualSubjects) {
		this.actualSubjects = actualSubjects;
	}

	public List<Subject> getAvailableSubjects() {
		return availableSubjects;
	}

	public void setAvailableSubjects(List<Subject> availableSubjects) {
		this.availableSubjects = availableSubjects;
	}

	public String getCertificate() {
		return certificate;
	}

	public void setCertificate(String certificate) {
		this.certificate = certificate;
	}

	public List<Course> getCourses() {
		return courses;
	}

	public void setCourses(List<Course> course) {
		this.courses = course;
	}

	public String getCurriculum() {
		return curriculum;
	}

	public void setCurriculum(String curriculum) {
		this.curriculum = curriculum;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getIdBody() {
		return idBody;
	}

	public void setIdBody(Long idBody) {
		this.idBody = idBody;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Instalment> getInstalments() {
		return instalments;
	}

	public void setInstalments(List<Instalment> instalments) {
		this.instalments = instalments;
	}

	public List<Long> getChosenSubjectsAsList() {
		List<Long> response = new ArrayList<Long>();

		if (chosenSubjects.trim().equals(""))
			return response;

		String[] aux = this.chosenSubjects.split(";");
		for (int i = 0; i < aux.length; i++) {
			response.add(Long.parseLong(aux[i]));
		}

		return response;
	}

	public String getChosenSubjects() {
		return chosenSubjects;
	}

	public void setChosenSubjects(String chosenSubjects) {
		this.chosenSubjects = chosenSubjects;
	}

	public List<LabelValueBean> getBodyOptions() {
		return bodyOptions;
	}

	public void setBodyOptions(List<LabelValueBean> bodyOptions) {
		this.bodyOptions = bodyOptions;
	}

	public List<LabelValueBean> getCourseTypeCollection() {
		return courseTypeCollection;
	}

	public void setCourseTypeCollection(List<LabelValueBean> courseTypeCollection) {
		this.courseTypeCollection = courseTypeCollection;
	}

	public String getCourseTypeSelected() {
		return courseTypeSelected;
	}

	public void setCourseTypeSelected(String courseTypeSelected) {
		this.courseTypeSelected = courseTypeSelected;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public Resolution getNewResolution() {
		return newResolution;
	}

	public void setNewResolution(Resolution newResolution) {
		this.newResolution = newResolution;
	}

	public Resolution[] getResolutions() {
		return resolutions;
	}

	public void setResolutions(Resolution[] resolutions) {
		this.resolutions = resolutions;
	}

	public String getFirstDueDate() {
		return firstDueDate;
	}

	public void setFirstDueDate(String firstDueDate) {
		this.firstDueDate = firstDueDate;
	}
}
